package com.weigj.model;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Model;

public class HourContract extends Model<HourContract> {
	public static final HourContract dao = new HourContract();

	public HourContract findRentingContractByCar(String carNum)
	{
		HourContract hc = new HourContract();
		CarInfo car = CarInfo.dao.queryCarInfo(carNum);
		String sql = "select * from app_hourcontract t where t.carnum=? and t.conState=1 and t.backCarState=0 and t.settlement=0";
		hc = dao.findFirst(sql, car.getStr("INFOLICENSE"));
		return hc;
	}
	/**
	 * 根据部门id查找已还车 结算后欠款状态的合同
	 * @param deptId
	 * @return
	 */
	public List<HourContract> queryHasBackCar(String deptId)
	{
		List<HourContract> hcs = null;
		String sql = "select * from app_hourcontract t where t.usedeptid=? and t.settlement=2 and t.conState=1 and t.backCarState=1 order by t.startTime desc";
		hcs = dao.find(sql, deptId);
		return hcs;
	}
	public List<HourContractBackSett> queryAllHasBalanceSet(String num){
		List<HourContract> hcs = new ArrayList<HourContract>();
		String sql ="";
		if(num.length()>=17){
		sql = "select * from app_hourcontract h where h.conState=1 and h.contractNum like '"+num.substring(0,17)+"%' order by h.startTime desc";
		}else{
		sql = "select * from app_hourcontract h where h.conState=1 and h.contractNum like '"+num+"%' order by h.startTime desc";
		}
		hcs = dao.find(sql);
		List<HourContractBackSett> listHcb=new ArrayList<HourContractBackSett>();
		for (HourContract hc: hcs)
		{
			HourContractBackSett hcb= HourContractBackSett.dao.findById(hc.getBigDecimal("BACKSETT_ID"));
			listHcb.add(hcb);
		}
		return listHcb;
	}
	/**根据部门id查询所有待替换合同 已做完还车
	 * @param deptId
	 * @return List<HourContact>
	 */
	public List<HourContract> queryAllChangeCarContact(String deptId){
		List<HourContract> hcs = null;
		String sql = "select * from app_hourcontract h where h.conState=1 and h.usedeptid=? and h.SETTLEMENT=3 and h.id=(select max(id) from app_hourcontract where substr(contractNum,1,17)=substr(h.contractNum,1,17) and conState=1) order by h.startTime desc";
		hcs = dao.find(sql,deptId);
		return hcs;
	}
}
